﻿using UnityEngine;
using System.Collections;

public class GfxFieldTexture : MonoBehaviour {

	public WorldField field;
	public Texture2D tex;
	public MeshRenderer renderer;
	public int res = 32;


	void Start () {
	
		tex = new Texture2D (res, res, TextureFormat.ARGB32, false);
		for (int x = 0; x < res; x++) {
			for (int y = 0; y < res; y++) {
				tex.SetPixel (x, y, Color.gray);
			}
		}
		tex.Apply ();
		renderer.material.mainTexture = tex;
	}
	

	void Update () {
		for (int x = 0; x < res; x++) {
			for (int y = 0; y < res; y++) {
				float f = 1;
				float fx = ((x+0.5f)/res)-0.5f;
				float fy = ((y+0.5f)/res)-0.5f;
				Vector3 local = new Vector3(-fx, 0, -fy) + transform.position;
				f = field.getIntensity(transform.localToWorldMatrix * local);
				//f = (x % 2) * (y % 2);
				Color c = Color.white * f;
				tex.SetPixel (x, y, c);
			}
		}
		tex.Apply ();
		renderer.material.mainTexture = tex;
		renderer.material.color = Color.red;
	}
}
